package com.sinosoft.brokerchannel.base.utile;

 
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
/**
 * 需求名称: 根据数据库生成Model类
 * 类描述:[生成数据表对应实体类]
 * @author shilk
 * @date [2017年9月20日 下午5:23:13]
 * @version [v1.0]
 */
public class CreateModel {

	public static void main(String[] args) {

		Statement stmt = null;
		Connection conn = null;
		// 表名
		String tableName = "bas_sys_dict";
		// 作者名
		String createName = "shilk";
		// 表说明
		String info = "渠道配置";
		// 模块名
		String packageName = "base";

		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://10.0.8.6:3306/cxqddb", "gatewayInt", "gatewayInt");
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from " + tableName);
			int colNum = rs.getMetaData().getColumnCount();
			File file = new File("src/main/java/com/sinosoft/claimsChannel/" + packageName
					+ "/bins/" + getUpdateClassName(tableName) + ".java");
			 
			Writer fw = new OutputStreamWriter(new FileOutputStream(file),
					"UTF-8");
			fw.write("/**\r\n");
			fw.write(" * Tech Ltd.\r\n");
			fw.write(" */\r\n");
			fw.write("package com.sinosoft.brokerchannel." + packageName + ".bins;\r\n\r\n");
			for (int i = 1; i <= colNum; i++) {
				int columnType = rs.getMetaData().getColumnType(i);
				if (columnType == java.sql.Types.DATE) {
					fw.write("import java.util.Date;\r\n\r\n");
					break;
				}
			}
			fw.write("import lombok.Data;\r\n"); 
			/*fw.write("import org.hibernate.annotations.Type;\r\n\r\n");*/
			fw.write("/**\r\n");
			fw.write(" * @see(功能介绍): " + info + "信息\r\n");
			fw.write(" * @date(创建日期): "
					+ ToolUtils.formatDate(new Date(), "yyyy-MM-dd"));
			fw.write("\r\n * @author(创建人): " + createName);
			fw.write("\r\n */\r\n");
			fw.write("\r\n@Data\r\n"); 
			fw.write("public class " + getUpdateClassName(tableName)
					+ " {\r\n");
			for (int i = 1; i <= colNum; i++) {
				String type = "String";
				int columnType = rs.getMetaData().getColumnType(i);
				if (columnType == java.sql.Types.DATE) {
					type = "Date";
				} else if (columnType == java.sql.Types.NUMERIC) {
					if (rs.getMetaData().getPrecision(i) == 38) {
						type = "Integer";
					} else {
						type = "Double";
					}
				}
				fw.write("\r\n	/**  "+getColumnCommentByTableName(tableName,rs.getMetaData().getColumnName(i))+" */\r\n	");

				fw.write("private "
						+ type
						+ " "
						+ getHorizontalLineUpper(rs.getMetaData()
								.getColumnName(i).toLowerCase()) + ";\r\n");
			}
			fw.write("} ");
			fw.flush();
			fw.close();
			rs.close();
			System.out.println(tableName + ":生产成功!");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static Connection getMySQLConnection() throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		return DriverManager.getConnection("jdbc:mysql://10.0.8.18:3306/didihealthychannel", "liaimsChannel", "Gq42F2!3f");
	}

	/**
	 * 获得某表中所有字段的注释
	 * @param table
	 * return
	 * throws Exception
	 */
	private static String getColumnCommentByTableName(String table,String cloumn) throws Exception {
		String comment = "";
		Connection conn = getMySQLConnection();
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery("show full columns from " + table);
		System.out.println("【"+table+"】");
		while (rs.next()) {
			System.out.println(rs.getString("Field") + "\t:\t"+  rs.getString("Comment") );
			if(cloumn.equals(rs.getString("Field"))){
				comment = rs.getString("Comment");
				break;
			}
		}
		rs.close();
		stmt.close();
		conn.close();

		return comment;
	}

	/**
	 *  阿里不允许代码中出险任何未定义的常量
	 */
	private static String hengxian = "_";

	/**
	 * 	 
	 *  需求名称：[需求/BUG名称]
	 *  功能:[横线前第一个字母大写]
	 *  @author [@wem]
	 *  @date 2017年9月20日 下午5:29:27
	 *  功能说明：
	 *	   1.说明1；<br/>
	 *	   2.说明2。<br/>
	 *     ······
	 *  @param str
	 *  return
	 */
	private static String getHorizontalLineUpper(String str) {
		str = str.substring(2); 
		String lowerStr = str.toLowerCase();
		// while (lowerStr.indexOf("_") != -1) {
		while (lowerStr.contains(hengxian)) {
			int index = lowerStr.indexOf(hengxian);
			lowerStr = lowerStr.replaceFirst(hengxian, "");
			lowerStr = lowerStr.substring(0, index)
					+ lowerStr.substring(index, index + 1).toUpperCase()
					+ lowerStr.substring(index + 1);
		}
		return lowerStr;
	}
	/**
	 * 	 
	 *  需求名称：[需求/BUG名称]
	 *  功能:[更新类名称]
	 *  @author [@wem]
	 *  @date 2017年9月20日 下午5:27:10
	 *  功能说明：
	 *	   1.说明1；<br/>
	 *	   2.说明2。<br/>
	 *     ······
	 *  @param str
	 *  return
	 */
	private static String getUpdateClassName(String str) {
		String lowerStr = str.toLowerCase();
		int index = lowerStr.indexOf(hengxian);
		lowerStr = lowerStr.replaceFirst(hengxian, "");
		lowerStr = lowerStr.substring(0, 1).toUpperCase()
				+ lowerStr.substring(1,index)
				+ lowerStr.substring(index, index + 1).toUpperCase()
				+ lowerStr.substring(index + 1);
		while (lowerStr.contains(hengxian)) {
			index = lowerStr.indexOf(hengxian);
			lowerStr = lowerStr.replaceFirst(hengxian, "");
			lowerStr = lowerStr.substring(0, index)
					+ lowerStr.substring(index, index + 1).toUpperCase()
					+ lowerStr.substring(index + 1);
		}
		return getFirstUpper(lowerStr);
	}
	/**
	 * 第一个字母大写
	 */
	private static String getFirstUpper(String str) {
		return str.substring(0, 1).toUpperCase() + str.substring(1);
	}
	
}
